package de.justphil.tcg.tcgserver.commons.util;

public class OTPGenerator {
	
	public static String otp(long l) {
		String longStr = String.valueOf(l);
		String time = String.valueOf(System.currentTimeMillis());

		StringBuilder sb = new StringBuilder(longStr.length() + time.length());

		String s = paddedSdbmHash(sb.append(longStr).append(time).toString());
		// System.out.println(s);

		sb.delete(0, sb.capacity());

		sb.append(s.charAt(0)).append(s.charAt(3)).append(s.charAt(6))
				.append(s.charAt(9)).append(s.charAt(12)).append(s.charAt(15));

		// System.out.println(sb.toString());

		return sb.toString();
	}
	
	private static String paddedSdbmHash(String str) {
        long hash = 0;
        for (int c : str.toCharArray()) {
            hash = c + (hash << 6) + (hash << 16) - hash;
        }
        
        StringBuilder sb = new StringBuilder(16);
        sb.append(Long.toHexString(hash));
        
        int length = sb.length();
        while ( length < 16) {
        	sb.append(0);
        	length++;
        }
        
        return sb.toString();
    }

}
